﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace CMDBImportCIDLL
{


    public class MainProgress
    {
        public void Import(string server, string login, string password, Company company, string filePath, ShowMessageDelegate show, ImportOperator imoperator)
        {
            string resultStr1 = string.Empty;
            string resultStr2 = string.Empty;
            string resultStr3 = string.Empty;

            AnalyzeXML xml = new AnalyzeXML();
            Factory factory = new Factory();
            CommonExcelOperate excel = new CommonExcelOperate();

            List<Sheet> sheetList = xml.getExcelToDBSheetNameDic();

            List<ExcelData> excelDataList = excel.GetExcelData(filePath);


            if (excel.CheckExcelColumn(excelDataList, sheetList, out resultStr1))
            {
                throw new Exception(resultStr1);
            }
            if (excel.CheckCiNameRepeat(excelDataList, sheetList, out resultStr2))
            {
                throw new Exception(resultStr2);
            }
            if (excel.CheckRelationshipCiNameNotExist(excelDataList, sheetList, out resultStr3))
            {
                throw new Exception(resultStr3);
            }


            foreach (var sheet in sheetList)
            {
                if (sheet.SheetName != "关系表")
                {
                    DataTable dt = excelDataList.Find(delegate(ExcelData p) { return p.SheetName == sheet.SheetName; }).DataTable;
                    factory.ImportData(server, login, password, company, dt, sheet, show, imoperator);
                }
            }
            if (imoperator != ImportOperator.Delete)
            {
                DataTable dt2 = excelDataList.Find(delegate(ExcelData p) { return p.SheetName == "关系表"; }).DataTable;
                factory.ImportData(server, login, password, company, dt2, sheetList.Find(delegate(Sheet p) { return p.SheetName == "关系表"; }), show, imoperator);
            }
        }

    }

}
